package liu.z;

import java.util.Map;

import jsoup.Parse10Big;
import util.Dg;
import util.StringUtil;
import web.HtmlSource;
import android.R.integer;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.dutbbs.adapter.ListViewBaseAdaper;

public class Index extends Activity
{
    private ListView listView = null;
    
    private Context context = this;
    
    private int currentPage = 1;
    private int maxPage;
    
    public static final int next_menu_ID = Menu.FIRST;
    public static final int pre_menu_ID = Menu.FIRST+1;
    
    private String url;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_main);  
        TextView textView = (TextView)this.findViewById(R.id.titleMain);
        textView.setText(StringUtil.getShortTile(getIntent()));
        listView = (ListView) this.findViewById(R.id.MyListView);
        MyViewListListener listener = new MyViewListListener();
        listView.setOnItemClickListener(listener);
        url = getIntent().getStringExtra("url");
        HtmlSourseGeter h = new HtmlSourseGeter(this);
        h.execute(url);
    }

    class MyViewListListener implements OnItemClickListener 
    {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            Intent in = new Intent(context, testma.class);
            @SuppressWarnings("unchecked")
            Map<String,String> itemMap = (Map<String, String>) listView.getItemAtPosition(position);
            in.putExtra("url", itemMap.get("info"));
            in.putExtra("title", itemMap.get("title"));
            startActivity(in);
        }
    };
 

    class HtmlSourseGeter extends AsyncTask<String, integer, String>
    {

        private Context context;
        private ProgressDialog mProgressBar;

        public HtmlSourseGeter(Context context)
        {
            this.context = context;
            mProgressBar = new ProgressDialog(context);
        }

        @Override
        protected String doInBackground(String... params)
        {
            String result = null;
            try
            {
                result = HtmlSource.getHtml_data(params[0]);
            } catch (Exception e)
            {
                e.printStackTrace();
            }
//            System.out.println(result);
            try{
            	currentPage = Integer.parseInt(result.split("页</a>\\|<a class=\"plant\">")[1].split("/")[0]);
            	maxPage = Integer.parseInt(result.split("页</a>\\|<a class=\"plant\">")[1].split("/")[1].split("<")[0]);
            }catch(Exception e){
            	currentPage=1;
            	maxPage=1;
            	return result;
            } 	
            return result;
        }

        @Override
        protected void onPreExecute()
        {

            mProgressBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            mProgressBar.show();
        }

        @Override
        protected void onPostExecute(String date)
        {
            if (date == null)
            {
                new Dg().showDg(context, "get result is null");
                return;
            }
            BaseAdapter adapter = new ListViewBaseAdaper(context, Parse10Big.get10BigNews(date));
            mProgressBar.dismiss();
            listView.setAdapter(adapter);
        }

    }
    public boolean onCreateOptionsMenu(Menu menu){
		super.onCreateOptionsMenu(menu);
		menu.add(0, next_menu_ID, 0, "下一頁");
		menu.add(0, pre_menu_ID, 0, "上一頁");
		return true;
	}
    public boolean onOptionsItemSelected(MenuItem item){
		switch(item.getItemId()){
		case next_menu_ID:
			currentPage++;
			HtmlSourseGeter h1 = new HtmlSourseGeter(this);
			h1.execute(url+"?p="+currentPage);
			break;
		case pre_menu_ID:
			currentPage--;
			HtmlSourseGeter h2 = new HtmlSourseGeter(this);
			h2.execute(url+"?p="+currentPage);
			break;
		}
		return true;
    }
    public boolean onMenuOpened(int featureId, Menu menu){
    	menu.findItem(pre_menu_ID).setEnabled(true);
    	menu.findItem(next_menu_ID).setEnabled(true);
    	if(currentPage==1)
			menu.findItem(pre_menu_ID).setEnabled(false);
		if(currentPage==maxPage)
			menu.findItem(next_menu_ID).setEnabled(false);
		return super.onMenuOpened(featureId, menu);
    }
//    public boolean onPrepareOptionsMenu(Menu menu) {
//    	if(i==1)
//			menu.findItem(pre_menu_ID).setEnabled(false);
//		if(i==max)
//			menu.findItem(next_menu_ID).setEnabled(false);
//		return super.onPrepareOptionsMenu(menu);
//    }
}
